 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace reverseWork
{
    class reverseWord
    {
        static char[] ReverseWord(char [] chuoi)
        {
            int tokenRead= chuoi.Length - 1;
            int wordEnd, wordRead;
            int writePos = 0;
            // tao mang dem de luu tam ki tu da doc
            char[] buffer = new char[chuoi.Length];

            while(tokenRead >= 0)
            {
                if(chuoi[tokenRead]== ' ')
                {
                    buffer[writePos++] = chuoi[tokenRead--];
                }
                else
                {
                    wordEnd = tokenRead;
                    while(tokenRead >=0 && chuoi[tokenRead]!=' ')
                    {
                        tokenRead--;

                    }
                    wordRead = tokenRead + 1;
                    while(wordRead <= wordEnd)
                    {
                        buffer[writePos++] = chuoi[wordRead++];
                    }
                }

            }
            return buffer;

        }
        static void Main(string[] args)
        {
            Console.WriteLine("Input string: ");
            string chuoi = Console.ReadLine();
            char[] chuoiout = ReverseWord(chuoi.ToCharArray());
            foreach(char s1 in chuoiout)
            {
                Console.Write(s1);
            }
            Console.WriteLine();
            

        }
    }
}
